﻿using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Data.SqlClient;
using Texfina.Core.Data;
using Texfina.Entity.Sy;
using Texfina.Entity.Sy;
using Texfina.DOMnt.Sy;

namespace Texfina.DOMnt.Sy
{
    /// <summary>
    /// Data Object para Mantenimiento a la Tabla: SY_Auditoria (SY_Auditoria)
    /// </summary>
    /// <remarks></remarks>
    public class Auditoria : DataObjectBase, IDOUpdate
    {

        #region IDOUpdate Members

        public object[] Insert(IEntityBase value)
        {

            EAuditoria objE = (EAuditoria)value;

            //----------- Generacion de Codigos ------------------
            TablaCorre objDO = new TablaCorre();
            ETablaCorre objECorre = new ETablaCorre();

            objECorre.IdPeriodo = objE.IdPeriodo;
            objECorre.IdEmpresa = objE.IdEmpresa;
            objECorre.IdTabla = "SY_Auditoria";

            string strNewCode = objDO.GenNewCode(objECorre);
            objE.IdEvento = strNewCode;

            //----------------------------------------------------
            ArrayList arrPrm = BuildParamInterface(objE);

            try
            {
                int intRes = this.ExecuteNonQuery("SY_Auditoria_mnt01", arrPrm);

                return new object[] { objE.IdPeriodo };

            }
            catch (Exception ex)
            {

                ServerObjectException objEx = (ServerObjectException)this.GetException(MethodBase.GetCurrentMethod(), ex);
                throw objEx;

            }

        }

        public int Update(IEntityBase value)
        {

            EAuditoria objE = (EAuditoria)value;

            try
            {

                ArrayList arrPrm = BuildParamInterface(objE);

                int intRes = this.ExecuteNonQuery("SY_Auditoria_mnt02", arrPrm);

                return intRes;

            }
            catch (Exception ex)
            {

                ServerObjectException objEx = (ServerObjectException)this.GetException(MethodBase.GetCurrentMethod(), ex);
                throw objEx;

            }

        }

        public int Delete(IEntityBase value)
        {

            EAuditoria objE = (EAuditoria)value;

            try
            {

                ArrayList arrPrm = new ArrayList();


                arrPrm.Add(DataHelper.CreateParameter("@pidPeriodo", SqlDbType.Char, 4, objE.IdPeriodo));
                arrPrm.Add(DataHelper.CreateParameter("@pidEmpresa", SqlDbType.Char, 3, objE.IdEmpresa));
                arrPrm.Add(DataHelper.CreateParameter("@pidEvento", SqlDbType.Char, 10, objE.IdEvento));

                int intRes = this.ExecuteNonQuery("SY_Auditoria_mnt03", arrPrm);

                return intRes;

            }
            catch (Exception ex)
            {

                ServerObjectException objEx = (ServerObjectException)this.GetException(MethodBase.GetCurrentMethod(), ex);
                throw objEx;

            }

        }

        private ArrayList BuildParamInterface(EAuditoria value)
        {

            ArrayList arrPrm = new ArrayList();

            arrPrm.Add(DataHelper.CreateParameter("@pidPeriodo", SqlDbType.Char, 4, value.IdPeriodo));
            arrPrm.Add(DataHelper.CreateParameter("@pidEmpresa", SqlDbType.Char, 3, value.IdEmpresa));
            arrPrm.Add(DataHelper.CreateParameter("@pidEvento", SqlDbType.Char, 10, value.IdEvento));
            arrPrm.Add(DataHelper.CreateParameter("@pfeEvento", SqlDbType.DateTime, value.FeEvento));
            arrPrm.Add(DataHelper.CreateParameter("@pdsEvento", SqlDbType.VarChar, 3000, value.DsEvento));
            arrPrm.Add(DataHelper.CreateParameter("@pidTipoEvento", SqlDbType.Char, 3, value.IdTipoEvento));
            arrPrm.Add(DataHelper.CreateParameter("@pidTabla", SqlDbType.VarChar, 100, value.IdTabla));
            arrPrm.Add(DataHelper.CreateParameter("@pdsPrimaryKey", SqlDbType.VarChar, 500, value.DsPrimaryKey));
            arrPrm.Add(DataHelper.CreateParameter("@pidUser", SqlDbType.Char, 5, value.IdUser));
            arrPrm.Add(DataHelper.CreateParameter("@pidLogon", SqlDbType.VarChar, 100, value.IdLogon));
            arrPrm.Add(DataHelper.CreateParameter("@pidLOCAL_ADDR", SqlDbType.VarChar, 100, value.IdLOCAL_ADDR));
            arrPrm.Add(DataHelper.CreateParameter("@pidREMOTE_ADDR", SqlDbType.VarChar, 100, value.IdREMOTE_ADDR));
            arrPrm.Add(DataHelper.CreateParameter("@pidREMOTE_HOST", SqlDbType.VarChar, 100, value.IdREMOTE_HOST));
            arrPrm.Add(DataHelper.CreateParameter("@pidREMOTE_USER", SqlDbType.VarChar, 100, value.IdREMOTE_USER));

            return arrPrm;

        }

        #endregion

    }
}
